package com.vecspace.dayu.code.ui.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.vecspace.database.dao.BaseDao;
import com.vecspace.dayu.code.ui.pojo.DataGridColumn;

/** DAYU-页面数据表格列,操作数据库的dao类
 * @author: wanghua
 */
@Repository("dayu_dataGridColumnDao")
public class DataGridColumnDao extends BaseDao {
	/** 根据ID查询 DAYU-页面数据表格列
	 * @param id   主键值
	 * @return DataGridColumn
	 * @throws Exception
	 */
	public DataGridColumn getDataGridColumn(Long id) throws Exception {
		DataGridColumn dataGridColumn = null;
		if (id != null && id != 0) {

			dataGridColumn = this.queryObject(DataGridColumn.class, id);
			this.evict(dataGridColumn);
		}
		return dataGridColumn;
	}
	
	/**根据表格id或者表格的所有字段
	 * 
	 * @param datagridId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<DataGridColumn> getDataGridColumns(Long datagridId) throws Exception {
		//List<DataGridColumn> dataGridColumns = this.queryObjectList("from DataGridColumn where datagridId=?0 order by ordnum", new Object[] {datagridId});
		String sql = "select t1.ID,t1.DATAGRID_ID,t1.COLUMN_ID,t2.COLUMN_NAME,t1.COLUMN_LABEL,t1.COLUMN_GROUP,t1.COLUMN_WIDTH,t1.CELL_TEXT_TYPE,t1.FONT_STYLE,t1.HYPERLINK,t1.RENDER_SCRIPT,t1.VISIBLE_STATUS,t1.FIXED,t1.MERGE_CELL,\r\n" + 
				"t1.EDIT_ABLE,t1.INPUT_ELEMENT_TYPE,t1.EDIT_MENU,t1.AGGREGATE_FUNC,t1.AGGREGATE_GROUP,t1.QUERY_MULTI_VALUE,t1.IS_QUERY_FIELD,t1.QUERY_SQL_OPERATOR,t1.QUERY_VISIBLE_STATUS,t1.QUERY_INPUT_ELEMENT_TYPE,t1.QUERY_REQUIRED,t1.ORDNUM,t1.CREATE_TIME,t1.CREATE_USER \r\n" + 
				"from DAYU_CODE_ui_data_grid_column t1 left join DAYU_CODE_data_column t2 on t1.COLUMN_ID = t2.ID where t1.DATAGRID_ID = '" + datagridId + "' order by t1.ORDNUM";
		List<Map<String, Object>> rows = queryDataRowMapList(sql);
		List<DataGridColumn> dataGridColumns = new ArrayList<>();
		for(Map<String, Object> row : rows){
			DataGridColumn dataGridColumn = new DataGridColumn();
			dataGridColumn.parseFrom(row);
			//dataGridColumn.setColumnName(rowmap.getString("COLUMN_NAME"));
			dataGridColumns.add(dataGridColumn);
		}
		
		return dataGridColumns;
	}
	
	/** 插入或修改 DAYU-页面数据表格列 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void saveDataGridColumn(DataGridColumn item) throws Exception {
		this.saveOrUpdateObject(item);
		this.evict(item);
	}
	
	/**更新单个字段的值
	 * @param columnName
	 * @param id
	 * @param value
	 * @return 返回更新的记录数
	 * @throws Exception 
	 * @author wanghua
	 */
	public int updateValue(Long id, String columnName, Object value) throws Exception {
		String tableName = "DAYU_CODE_UI_DATA_GRID_COLUMN";
		String idColumn = "ID";
		return super.updateValue(tableName, idColumn, columnName, id, value);
	}

	/** 根据ID删除 DAYU-页面数据表格列 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void deleteDataGridColumn(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(DataGridColumn.class, id);
		}
	}
}
